<?php
/**
 * Created by PhpStorm.
 * User: 64
 * Date: 2017/3/23 0023
 * Time: 14:26
 */
namespace app\web\controller;
use app\src\base\helper\ConfigHelper;
use app\src\user\action\LoginAction;
use app\src\user\logic\UcenterMemberLogic;
use app\web\api\InsuranceApi;
use app\web\api\AccountScoreApi;
use app\web\api\WithdrawDepositApi;
use app\web\api\UserApi;
use app\web\api\InviteCodeApi;
use app\src\admin\helper\ShortLinkHelper;
use app\web\api\SellerApi;
use app\src\admin\api\po\SecurityCodePo;
use app\src\admin\api\SecurityCodeApi;
use app\src\securitycode\model\SecurityCode;
use app\src\admin\helper\AdminSessionHelper;
use app\src\securitycode\action\SecurityCodeVerifyAction;
use app\src\user\logic\MemberConfigLogic;
use think\Db;
use app\web\api\FriendApi;
use app\src\weixin\action\WxSendAction;
class User extends  Weixin{
    protected function _initialize() {
        parent::_initialize();
        if (session("?memberinfo")) {
            $this->memberinfo = session("memberinfo");
        }else{
                $this->redirect('index/index');
        }
    }
//个人资料
    public function userInfo(){
        $this->assignTitle('编辑个人资料');
        if (IS_POST) {
            $uid = $this->memberinfo['uid'];
            $nickname = $this->_param('nickname');
            $sex = $this->_param('sex');
            $sign = $this->_param('sign');
            $head = $this->_param('head');
            $result = UserApi::update($uid, $nickname, $sex, $sign, $head, $email = '');
           // dump($result);exit;
            if ($result['status']) {
                $action = new LoginAction();
                $uid=$this->memberinfo['uid'];
                $result = $action->loginByUID($uid,'device_id_um','pc','role_user');
                if($result['status']) {
                    session("memberinfo", $result['info']);
                }
                $this->success('修改成功', '', $result['info']);
            } else {
                $this->error('修改失败', '', $result['info']);
            }
        }
        $memberinfo = $this->memberinfo;
        $this->assign('memberinfo',$memberinfo);
        return $this->fetch();
    }

    public function protocalorder(){
        $this->assignTitle('车险订单');
        $uid = $this->memberinfo['uid'];
        $result=InsuranceApi::userApplys($uid);
//dump($result);exit;
        $this->assign('order_list',$result['info']);
        return $this->fetch();
    }

    public function protocalupload(){
        $this->assignTitle('提交凭证');
        if(IS_POST) {
            $id = $this->_param('id');
            $img = $this->_param('img');
            $result = InsuranceApi::addPayImg($id,$img);
            if (!$result['status']) {
                $this->error('上传失败','');
            }else{
                $this->success('凭证上传成功', '',$result['info']);
            }
        }
        $id = $this->_param('id');
        $this->assign('id',$id);
        return $this->fetch();
    }


    public function discount(){


        return $this->fetch();
    }
    public function person(){

        return $this->fetch();
    }





  //积分
    public function integral(){
        $this->assignTitle('我的收入');
     //   dump($this->memberinfo);
        $result=AccountScoreApi::getInfo('0',$this->memberinfo['uid']);

     //   dump($result);exit;
        if($result['status']) {
            $this->assign('score', $result['info']['score1']);

        }
        return $this->fetch();
    }

    public function integral_data(){
        $page = $this->_param('page');
        $type = $this->_param('type');
        $uid =$this->memberinfo['uid'];
        $result=AccountScoreApi::getAccounts($uid,'0',$page,10,$type);
     //   dump($result);
        if ($result['status']) {
            $this->success('数据获取成功', '', $result['info']['list']);
        } else {
            $this->error('数据获取失败', '', $result['info']);
        }
        return $this->fetch();
    }

    //亲朋好友
    public function friend(){
        $this->assignTitle('亲朋好友');
        return $this->fetch();
    }

    //亲朋好友data
    public function friend_data(){
        $page = $this->_param('p');
        $types = $this->_param('types');
        $uid =$this->memberinfo['uid'];

        $result =  SellerApi::getFriend($uid,$types,$page);
//       dump($result);
        if ($result['status']) {
            $this->success('数据获取成功', '', $result['info']['list']);
        } else {
            $this->error('数据获取失败', '', $result['info']);
        }
        return $this->fetch();
    }


    //消费
    public function consumption(){
        $this->assignTitle('消费记录');
        return $this->fetch();
    }

    //消费记录
    public function consumption_data(){
        $page = $this->_param('page');

        $result=SellerApi::queryConsume($this->memberinfo['uid'],$page);
        if ($result['status']) {
            $this->success('数据获取成功', '', $result['info']['list']);
        } else {
            $this->error('数据获取失败', '', $result['info']);
        }
        return $this->fetch();
    }

    public function erweima(){
        $this->assignTitle('名片/支付');
        $SignPackage = $this->wxapi->getSignPackage();
        $this->assign('SignPackage', $SignPackage);
        $uid = $this->memberinfo['uid'];
        $code=InviteCodeApi::getCode('0',$uid);
        if(!$code['status']) $this->error($code['info']);
//dump($code);exit;
        $url = SITE_URL.url('index/index',array('invite_code'=>$code['info']['invite_code'],'uid'=>$uid));
        $this->assign('invite_code', $code['info']['invite_code']);
        $this->assign('memberinfo',$this->memberinfo);
        $this->assign('url', $url);
        return $this->fetch();
    }

    public function index(){
        $this->assignTitle('个人中心');
        $this->assign('head', $this->memberinfo['head']);
        $result=AccountScoreApi::getInfo('0',$this->memberinfo['uid']);
        $CUSTOMER_PHONE = ConfigHelper::getValue('CUSTOMER_PHONE');
        $tel = $CUSTOMER_PHONE['tel'];
        $this->assign('tel', $tel);
        $countInfo = SellerApi::countInfo($this->memberinfo['uid'],6);
        if($countInfo['status']) {
            $this->assign('countInfo', $countInfo['info']);
        }
        if($result['status']) {
            $this->assign('score', $result['info']['score1']);
            $this->assign('nickname', $this->memberinfo['nickname']);
            if(preg_match("/^1\d{10}$/",$this->memberinfo['mobile'])){
                $this->assign('mobile', $this->memberinfo['mobile']);
            }else{
                $this->assign('mobile', '');
            }

        }
        return $this->fetch();
    }

    public function tixian(){
        $this->assignTitle('提现');
        if (IS_POST) {
            $account_id = $this->memberinfo['uid'];
            $score = $this->_param('score');
            $score = $score*100;
            if($score<0){
                $this->error('提现金额不能少于0元', '','');
            }
            $account_type = '0';
            $bank_card_no = $this->_param('bank_card_no');
            $mobile = $this->_param('mobile');
            $name = $this->_param('name');
            $pca = $this->_param('pca');
            $kh = $this->_param('kh');
            $bank_card_name = $this->_param('bank_card_name');
            $bank_card_name = $pca.','.$bank_card_name.','.$kh;
            $result =  WithdrawDepositApi::apply($account_id,$score,$bank_card_no,$bank_card_name,$account_type,$mobile,$name);
            if ($result['status']) {
                $this->success('添加成功', '', $result['info']);
            } else {
                $this->error($result['info'], '', $result['info']);
            }
        }
        $result=AccountScoreApi::getInfo('0',$this->memberinfo['uid']);
        if($result['status']) {
            $this->assign('score', $result['info']['score1']);

        }
        return $this->fetch();
    }

    public function test(){
        $url='http://api.zw.8raw.com/web.php/staticpage/fh';
        $url = (new ShortLinkHelper)->sinaShortenUrl($url);
        dump($url);
    }

    public function binding(){

        if (IS_GET) {
            $this->assignTitle('绑定手机');
                return $this->fetch();
        }

        $memberinfo=$this->memberinfo;
        $phone = $this->_param('mobile');
        $code = $this->_param('code');
        $verify=UserApi::verify('+86',$phone,$code,3);
        //dump($verify);exit;
        if(!$verify['status']) $this->error('验证码错误','');
        if($phone) {
            Db::startTrans();
            $entity = array(
                'mobile' => $phone
            );
            $has=(new UcenterMemberLogic())->getInfo($entity);
            if(!empty($has['info'])) {
                $this->error('该手机已被绑定','');
            }
            $flag = true;
            $result =  (new UcenterMemberLogic())->save(['id' => $memberinfo['uid']],$entity);
            if(!$result['status'])  $flag = false;
            $entity = array(
                'phone_validate' => 1
            );
            $result = (new MemberConfigLogic())->save(['uid' => $memberinfo['uid']],$entity);
            if(!$result['status'])  $flag = false;
            if($flag){
                Db::commit();
                $this->success('更新成功','user/index');
            }else{
                Db::rollback();
                $this->error('更新失败','');
            }
        }
    }

    public function getcode(){
        $code_type = 3;

        $code=$this->_param('code');
        $result = $this -> check_verify($code);
        if(!$result['status']){
            $this->error('获取失败验证码错误', '',$result['info']);
        }
        $mobile=$this->_param('mobile');
        $result = UserApi::code($mobile,$code_type);
        if($result['status']) {
            $this->success('短信已发送', '',$result['info']);
        }else{
            $this->error('发送失败', '',$result['info']);
        }

    }
    private function check_verify($code)
    {
        $acceptor = AdminSessionHelper::getSessionId();
        $po = new SecurityCodePo();
        $po->setAcceptor($acceptor);
        $po->setCodeType(SecurityCode::TYPE_FOR_LOGIN);
        $po->setCode($code);
        $api = new SecurityCodeApi();
        return $api->check($po);
    }

}